package com.xingxing.util;

import com.mysql.cj.util.StringUtils;

/**
 * @program: IPersistence_test
 * @description:
 * @author: xinghaozhao
 * @create: 2020-09-14
 */
public class CamelUnderlineUtil {

    private static final char UNDERLINE ='_';
    public static String camelToUnderline(String param) {
        if (StringUtils.isNullOrEmpty(param)) {
            return "";
        }
        String titleCase = titleCase(param);
        StringBuilder sb = new StringBuilder();
        int len = titleCase.length();
        for (int i = 0; i < len; i++) {
            char c = titleCase.charAt(i);
            if (Character.isUpperCase(c)) {
                sb.append(UNDERLINE);
                sb.append(Character.toLowerCase(c));
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    public static String underlineToCamel(String param){
        if (StringUtils.isNullOrEmpty(param)) {
            return "";
        }
        String titleCase = titleCase(param);
        StringBuilder sb = new StringBuilder();
        int len = titleCase.length();
        for (int i = 0; i < len; i++) {
            char c = titleCase.charAt(i);
            if (c==UNDERLINE) {
                if(++i<len){
                    sb.append(Character.toUpperCase(titleCase.charAt(i)));
                }
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    public static String titleCase (String word){
        if (!StringUtils.isNullOrEmpty(word)) {
            char[] cs=word.toCharArray();
            cs[0]-=32;
            word = String.valueOf(cs);
        }
        return word;
    }
}
